﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;

using Google.GData.Photos;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Extensions.Location;

using OpenSocialPlugin.General;
using OpenSocialPlugin.Providers.Sites.PicasaWeb;


namespace OpenSocialPlugin.Providers
{
    class PicasaWeb : BaseSite, ISocialSite
    {

        PicasaService service = new PicasaService("SocialPluginForMediaPortal");
    
        SiteSettings _settings = new SiteSettings();

        public PicasaWeb()
        {
            _settings.Load();
            _capabilities = new List<CapabilitiesEnum>();
            settingForm = new SettingControl();
            settingForm.settings = _settings;
            _capabilities.Add(CapabilitiesEnum.CanSearch);
            _capabilities.Add(CapabilitiesEnum.HasFriend);
            _capabilities.Add(CapabilitiesEnum.HasPhotos);
            _capabilities.Add(CapabilitiesEnum.HasExtras);
            _capabilities.Add(CapabilitiesEnum.HasOtherFriend);
            _capabilities.Add(CapabilitiesEnum.HasOtherPhotos);

        }
        private List<OSP_Photo> SearchPhotos(string search,int type)
        {
            PhotoQuery query = null; 
            switch (type)
            {
                case 0:
                    {
                        query = new PhotoQuery("http://picasaweb.google.com/data/feed/api/all");
                        query.Query = search;
                    }
                    break;
                case 1:
                    {
                        query = new PhotoQuery("http://picasaweb.google.com/data/feed/api/all");
                        query.Tags = search;
                    }
                    break;
                default:
                    return new List<OSP_Photo>();
                    break;
            }
            
            //query.NumberToRetrieve = 10;

            PicasaFeed feed = service.Query(query);
            return Feed2Photos(feed, new OSP_Album());
        }

        #region ISocialSite Members

      public string GetPersonInfo(OSP_Person person)
      {
        throw new NotImplementedException();
      }

      public string GetPhotoInfo(OSP_Photo photo)
      {
        throw new NotImplementedException();
      }

      public List<string> SearchNames
        {
            get
            {
                List<string> list = new List<string>();
                list.Add("Search by photo name");
                list.Add("Search by photo tag");
                return list;
            }
        }

      
        public List<string> ExtrasNames
        {
          get
          {
            List<string> list = new List<string>();
            list.Add("Featured photos");
            list.Add("New photos");
            return list;
            ;
          }
        }

        public GeneralResponse Extras(string serchtxt, int type)
        {
          GeneralResponse resp = new GeneralResponse();
          PhotoQuery query = null;
          PicasaFeed feed;
          switch (type)
          {
            case 0:

              query = new PhotoQuery("http://picasaweb.google.com/data/feed/api/featured/");
              feed = service.Query(query);
              resp.Response = Feed2Photos(feed, new OSP_Album());
              resp.Title = "Featured photos";
              return resp;
            case 1:
              query = new PhotoQuery("http://picasaweb.google.com/data/feed/api/all");

              feed = service.Query(query);
              resp.Response = Feed2Photos(feed, new OSP_Album());
              resp.Title = "New photos";

              return resp;
            default:
              return null;
          }
        }

      public GeneralResponse Search(string serchtxt, int type)
        {
            GeneralResponse res = new GeneralResponse();
            switch (type)
            {
                case 0:
                    {
                        res.Response = SearchPhotos(serchtxt, type);
                        res.Title = string.Format("Search result for {0}", serchtxt);
                    }
                    break;
                case 1:
                    {
                        res.Response = SearchPhotos(serchtxt, type);
                        res.Title = string.Format("Search result for tag(s) {0}", serchtxt);
                    }
                    break;
                default:
                    break;
            }
            return res;
        }


        public GeneralResponse Home()
        {
            GeneralResponse res = new GeneralResponse();
            OSP_Person pers = Login();
            if (pers == null)
              return Extras("", 1);
            List<OSP_Album> list = GetAlbums(pers);
            res.Response = list;
            res.Title = string.Format("Albums of {0}", pers);
            return res;
        }

        public List<OSP_Person> GetFriends(OSP_Person person)
        {
          List<OSP_Person> list = new List<OSP_Person>();
          PicasaQuery query =
            new PicasaQuery("http://picasaweb.google.com/data/feed/api/user/" + person.Id + "/contacts");
          query.KindParameter = "user";
          PicasaFeed feed = (PicasaFeed) service.Query(query);
          foreach (PicasaEntry entry in feed.Entries)
          {
            OSP_Person pers = new OSP_Person();
            pers.Id = entry.GetPhotoExtension("user").Value; 

            //many properties are in the Atom entry itself
            pers.Name = entry.GetPhotoExtension("nickname").Value;
            //but they are also in the gphoto namespace as well
            //Console.WriteLine(entry.getPhotoExtension("user").Value);
            //Console.WriteLine(entry.getPhotoExtension("nickname").Value);

            pers.Thumb = entry.GetPhotoExtension("thumbnail").Value;
            list.Add(pers);
          }
          return list;
        }

      public List<OSP_Album> GetAlbums(OSP_Person person)
        {
            List<OSP_Album> list = new List<OSP_Album>();
            AlbumQuery query = new AlbumQuery(PicasaQuery.CreatePicasaUri(person.Id));

            PicasaFeed feed = service.Query(query);

            foreach (PicasaEntry entry in feed.Entries)
            {
                Console.WriteLine(entry.Title.Text);
                AlbumAccessor ac = new AlbumAccessor(entry);
                OSP_Album al = new OSP_Album(ac.Id, person.Id);
                al.NumPhotos = (int)ac.NumPhotos;
                al.Title = ac.AlbumTitle;
                if (entry.Media.Thumbnails.Count > 0)
                {
                    al.Thumb = entry.Media.Thumbnails[0].Url;
                }
                list.Add(al);
            }
            return list;
        }

        private List<OSP_Photo> Feed2Photos(PicasaFeed feed, OSP_Album album)
        {
            List<OSP_Photo> list = new List<OSP_Photo>();
            foreach (PicasaEntry entry in feed.Entries)
            {
                OSP_Photo ph = new OSP_Photo();
                ph.OwnerId = album.OwnerId;
                ph.AlbumId = album.Id;
                if (entry.Media.Thumbnails.Count > 2)
                {
                    ph.Thumb = entry.Media.Thumbnails[2].Url;
                }
               
                ph.Title = entry.Summary.Text;
                ph.Url = entry.Content.AbsoluteUri;
                list.Add(ph);
            }
            return list;
        }

        public List<OSP_Photo> GetPhotos(OSP_Album album)
        {
            PhotoQuery query = new PhotoQuery(PicasaQuery.CreatePicasaUri(album.OwnerId, album.Id));
            
            PicasaFeed feed = service.Query(query);
            return Feed2Photos(feed, album);

        }

        public void Init()
        {

        }

        public OSP_Person Login()
        {
          if (string.IsNullOrEmpty(_settings.User))
            return null;
            try
            {
                service.setUserCredentials(_settings.User, _settings.Password);
                return GetPerson(_settings.User);
            }
            catch
            {
                return null;
            }
        }

        private SettingControl settingForm;
        public Control SettingForm
        {
            get
            {
                return settingForm;
            }
        }

        public override string ToString()
        {
            return "PicasaWeb";
        }

        List<CapabilitiesEnum> _capabilities;
        public List<CapabilitiesEnum> Capabilities { get { return _capabilities; } }


        public OSP_Person GetPerson(string personId)
        {
            OSP_Person pers = new OSP_Person();
            PicasaQuery query = new PicasaQuery("http://picasaweb.google.com/data/feed/api/user/" + personId + "/contacts");
            query.KindParameter = "user";

            PicasaFeed feed = (PicasaFeed)service.Query(query);
            pers.Name = feed.Authors[0].Name;
            pers.Thumb = feed.Icon.AbsoluteUri;
            pers.Id = feed.Title.Text;
            //foreach (PicasaEntry entry in feed.Entries)
            //{
            //    //many properties are in the Atom entry itself
            //    pers.Name = entry.Authors[0].Name;
            //    //but they are also in the gphoto namespace as well
            //    //Console.WriteLine(entry.getPhotoExtension("user").Value);
            //    //Console.WriteLine(entry.getPhotoExtension("nickname").Value);

            //    pers.Thumb = entry.GetPhotoExtension("thumbnail").Value;
                
            //}
            return pers;
        }

        #endregion
    }
}
